
public class PatternProcessor {

	private PatternProcessor() {
	}
	
	public static double[][] densityPerRegion(int rows, int columns, int[][] pattern) {
		double[][] result = new double[rows][columns];
		int pWidth = pattern.length;
		int pHeight = pattern[0].length;
		int regWidth = pWidth / columns;
		int regHeight = pHeight / rows;
		
		for(int i=0; i!=columns; i++) {
			for(int j=0; j!=rows; j++) {
				result[i][j] = 0f;
			}
		}
		
		for(int x=0; x!=pWidth; x++) {
			int regX = x/regHeight;
			for(int y=0; y!=pHeight; y++) {
				int regY = y/regWidth;
				if (pattern[x][y] > 0)
					result[regX][regY]++;
			}
		}
		
		int regCount = (pWidth/columns) * (pHeight/rows);
		
		for(int i=0; i!=columns; i++) {
			for(int j=0; j!=rows; j++) {
				result[i][j] /= regCount;
			}
		}
		
		return result;
	}
	
	public static double percentTopHalf(int[][] pattern) {
		int pWidth = pattern.length;
		int pHeight = pattern[0].length;
		
		int countTotal = 0;
		int countAbove = 0;
		for(int y=0; y!=pHeight; y++) {
			for(int x=0; x!=pWidth; x++) {
				if (pattern[x][y] > 0) {
					countTotal++;
					if (y < pHeight/2)
						countAbove++;
				}
			}
		}
		if (countTotal == 0)
			return 0;
		return (double)countAbove / countTotal;
	}
	
	public static double percentLeftHalf(int[][] pattern) {
		int pWidth = pattern.length;
		int pHeight = pattern[0].length;
		
		int countTotal = 0;
		int countLeft = 0;
		for(int y=0; y!=pHeight; y++) {
			for(int x=0; x!=pWidth; x++) {
				if (pattern[x][y] > 0) {
					countTotal++;
					if (x < pWidth/2)
						countLeft++;
				}
			}
		}
		return (double)countLeft / countTotal;
	}
	
	public static double reflectedVertical(int[][] pattern) {
		int pWidth = pattern.length;
		int pHeight = pattern[0].length;
		
		int countReflected = 0;
		for(int y=0; y!=pHeight/2; y++) {
			for(int x=0; x!=pWidth; x++) {
				if (pattern[x][y] == pattern[x][(pHeight-1)-y]) {
					countReflected++;
				}
			}
		}
		
		return (double)countReflected / (pWidth*pHeight);
	}
	
	public static double reflectedHorizontal(int[][] pattern) {
		int pWidth = pattern.length;
		int pHeight = pattern[0].length;
		
		int countReflected = 0;
		for(int y=0; y!=pHeight; y++) {
			for(int x=0; x!=pWidth/2; x++) {
				if (pattern[x][y] == pattern[(pWidth-1)-x][y]) {
					countReflected++;
				}
			}
		}
		
		return (double)countReflected / (pWidth*pHeight);
	}

}
